<?php namespace Phpcmf\Model\Quan;

class Mall extends \Phpcmf\Model
{

    // 获取配置信息
    public function get_config($name, $siteid = SITE_ID) {

        $data = $this->db->table($siteid.'_store_config')->where('name', $name)->get()->getRowArray();
        if ($data) {
            $data = dr_string2array($data['value']);
            return $data;
        } else {
            $this->db->table($siteid.'_store_config')->insert([
                'name' => $name,
                'value' => '',
            ]);
            return [];
        }
    }

    // 存储配置信息
    public function save_config($name, $siteid = SITE_ID, $post = []) {

        $this->db->table($siteid.'_store_config')->where('name', $name)->update([
            'value' => dr_array2string($post)
        ]);
    }

    // 策略类型
    public function get_valuation() {
        return [
            1 => dr_lang('按数量'),
            2 => dr_lang('按重量'),
            3 => dr_lang('按体积'),
        ];
    }

    // 策略类型单位
    public function get_valuation_value() {
        return [
            1 => dr_lang('件'),
            2 => dr_lang('千克'),
            3 => dr_lang('立方'),
        ];
    }

    // 选择商品之后 获取商品价格信息
    public function get_goods_price_info($id, $sku = 0) {

        $data = $this->table(SITE_ID.'_store')->get($id);
        if (!$data) {
            return dr_return_data(0, dr_lang('商品不存在'));
        } elseif (!$data['is_sale']) {
            return dr_return_data(0, dr_lang('商品已经下架'));
        }

        $rt = [
            'id' => $data['id'],
            'cid' => $id.'-'.$sku,
            'title' => $data['title'],
            'url' => $data['url'],
        ];

        $price_sku = dr_string2array($data['price_sku']);
        if ($price_sku) {
            if (!$price_sku['value'][$sku]) {
                return dr_return_data(0, dr_lang('商品规格不存在'));
            }
            $rt['sku'] = dr_sku_name($sku, $price_sku);
            $rt['price'] = $price_sku['value'][$sku]['price'];
            $rt['quantity'] = $price_sku['value'][$sku]['quantity'];
        } else {
            $rt['sku'] = '';
            $rt['price'] = $data['price'];
            $rt['quantity'] = $data['price_quantity'];
        }

        return dr_return_data(1, 'ok', $rt);
    }


}